<#include "/includes/vars.inc"> 
<@pp.dropOutputFile />
<@pp.changeOutputFile name="${javaDir}${packageDir}/dao/support/AbstractDao.java" />

package ${project.@rootPackage}.dao.support;

import java.util.List;
import javax.persistence.EntityManager;

/**
 * AbstractDao class
 * <#if project.@author[0]??>@author ${project.@author}</#if>
 */
public abstract class AbstractDao<T> implements GenericDao<T> {

    private Class<T> entity;

    public AbstractDao(Class<T> entity) {
        this.entity = entity;
    }
    
    @Override
    public abstract EntityManager getEntityManager();

    @Override
    public boolean create(T entity) {
        getEntityManager().persist(entity);
        return true;
    }

    @Override
    public boolean delete(T entity) {
        if (getEntityManager().contains(entity)) {
            getEntityManager().remove(entity);
        } else {
            //T attached = getEntityManager().find(this.entity, entity.hashCode());
            T attached = getEntityManager().merge(entity);
            getEntityManager().remove(attached);
        }
        return true;
    }

    @Override
    public boolean update(T entity) {
        getEntityManager().merge(entity);
        getEntityManager().flush();
        return true;
    }

    @Override
    public void clear() {
        getEntityManager().clear();
    }

    @Override
    public void flush() {
        getEntityManager().flush();
    }

    @Override
    public T findById(Long id) {
        if (id == null) {
            return null;
        }
        return getEntityManager().find(this.entity, id);
    }

    @Override
    public List<T> findAll() {
        return getEntityManager().createQuery("SELECT o FROM " + entity.getSimpleName() + " o", this.entity).getResultList();
    }

    @Override
    public List<T> findAll(int firstResult, int maxResults) {
        return getEntityManager().createQuery("SELECT o FROM " + entity.getSimpleName() + " o", this.entity).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }
    
    /*
     public List<T> findAll() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        return getEntityManager().createQuery(cq).getResultList();
    }

    public List<T> findRange(int[] range) {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public int count() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
        cq.select(getEntityManager().getCriteriaBuilder().count(rt));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }
     */
}